package com.xuelang.pipeline.artifact.app.resource.impl;

import com.xuelang.pipeline.artifact.app.resource.InstanceComponentService;
import com.xuelang.pipeline.artifact.app.resource.RepositoryPackageService;
import com.xuelang.pipeline.artifact.app.resource.converter.RepositoryPackageDTOToRepositoryPackageConverter;
import com.xuelang.pipeline.artifact.client.common.request.PageParam;
import com.xuelang.pipeline.artifact.client.common.result.PageRecord;
import com.xuelang.pipeline.artifact.client.common.util.BizAssert;
import com.xuelang.pipeline.artifact.client.resource.dto.RepositoryComponentQuery;
import com.xuelang.pipeline.artifact.client.resource.dto.RepositoryPackageDTO;
import com.xuelang.pipeline.artifact.client.resource.dto.RepositoryPackageQuery;
import com.xuelang.pipeline.artifact.domain.resource.RepositoryComponent;
import com.xuelang.pipeline.artifact.domain.resource.RepositoryPackage;
import com.xuelang.pipeline.artifact.domain.resource.service.RepositoryComponentDomainService;
import com.xuelang.pipeline.artifact.domain.resource.service.RepositoryPackageDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 制品包服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class RepositoryPackageServiceImpl implements RepositoryPackageService {

    @Autowired
    RepositoryPackageDomainService repositoryPackageDomainService;
    @Autowired
    RepositoryComponentDomainService repositoryComponentDomainService;
    @Autowired
    InstanceComponentService instanceComponentService;

    @Override
    public PageRecord<RepositoryPackageDTO> pageList(PageParam<RepositoryPackageQuery> dtoPageParam) {
        RepositoryPackageQuery condition = dtoPageParam.getCondition();
        condition = condition == null ? new RepositoryPackageQuery() : condition;
        PageRecord<RepositoryPackage> pageRecord = repositoryPackageDomainService.pageList(
                dtoPageParam.getCurrentPage(),
                dtoPageParam.getPageSize(),
                condition);
        return new PageRecord<>(
                pageRecord.getCurrentPage(),
                pageRecord.getPageSize(),
                pageRecord.getTotalCount(),
                pageRecord.getTotalPages(),
                pageRecord.getRecords().stream().map(RepositoryPackageDTOToRepositoryPackageConverter.INSTANCE::repositoryPackageToRepositoryPackageDTO).collect(Collectors.toList()));
    }

    @Override
    public List<RepositoryPackageDTO> list(RepositoryPackageQuery repositoryPackageQuery) {
        return repositoryPackageDomainService.list(repositoryPackageQuery).stream().map(RepositoryPackageDTOToRepositoryPackageConverter.INSTANCE::repositoryPackageToRepositoryPackageDTO).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String packageUuid) {
        BizAssert.notNull(packageUuid, "id不能为空");
        RepositoryPackage repositoryPackage = repositoryPackageDomainService.query(packageUuid);
        List<RepositoryComponent> repositoryComponentList = repositoryComponentDomainService.list(RepositoryComponentQuery.builder().packageUuid(packageUuid).build());
        Boolean deleteRepositoryPackage = repositoryPackageDomainService.delete(packageUuid);
        Boolean deleteRepositoryComponents = repositoryComponentDomainService.deleteByPackageUuid(packageUuid);
        if (deleteRepositoryPackage) {
            instanceComponentService.deleteComponentOfRepository(repositoryPackage, repositoryComponentList);
        }
        return deleteRepositoryPackage;
    }


    @Override
    public RepositoryPackageDTO query(String packageUuid) {
        BizAssert.notNull(packageUuid, "id不能为空");
        RepositoryPackage repositoryPackage = repositoryPackageDomainService.query(packageUuid);
        return RepositoryPackageDTOToRepositoryPackageConverter.INSTANCE.repositoryPackageToRepositoryPackageDTO(repositoryPackage);
    }

}
